package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONObject;
import io.xxx.xbutler.data.RobotMapper;
import io.xxx.xbutler.domain.Callback;
import io.xxx.xbutler.domain.GenderType;
import io.xxx.xbutler.domain.Robot;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 修改机器人信息回调
 *
 * <pre>
 * {
 *     "nType": 1006,
 *     "vcMerchantNo": "",
 *     "vcRobotWxId": "",
 *     "vcRobotSerialNo": "",
 *     "vcSerialNo": "",
 *     "nResult": 1,
 *     "vcResult": "SUCCESS",
 *     "Data": {
 *         "vcRobotSerialNo": "",
 *         "vcRobotWxId": "",
 *         "nSex": 1,
 *         "vcWxAlias": "",
 *         "vcNickName": "",
 *         "vcBase64NickName": "",
 *         "vcHeadImgUrl": "",
 *         "vcWhatsUp": "",
 *         "vcBase64WhatsUp": "",
 *         "vcCountry": "",
 *         "vcProvince": "",
 *         "vcCity": ""
 *     }
 * }
 * </pre>
 */
@Slf4j
@Component
public class _1006Handler implements CallbackHandler {

    @Resource
    private RobotMapper robotMapper;

    @Override
    public String name() {
        return "修改机器人信息回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 1006;
    }

    @Override
    public void handle(Callback callback) {
        JSONObject data = (JSONObject) callback.getData();
        Robot robot = (Robot) new Robot()
                .setId(callback.getRobotId())
                .setGender(GenderType.valueOf(data.getIntValue("nSex")))
                .setName(data.getString("vcNickName"))
                .setBase64RemarkName(data.getString("vcBase64NickName"))
                .setAlias(data.getString("vcWxAlias"))
                .setAvatar(data.getString("vcHeadImgUrl"))
                .setWhatsUp(data.getString("vcWhatsUp"))
                .setBase64WhatsUp(data.getString("vcBase64WhatsUp"))
                .setProvince(data.getString("vcProvince"))
                .setCity(data.getString("vcCity"))
                .setCounty(data.getString("vcCountry"));
        robotMapper.updateById(robot);
    }
}
